<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
					<el-button type="primary" @click="getList">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data="tagsList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column prop="id" label="序号" width="70" />
			<el-table-column prop="title" label="标题" width="200" sortable/>
			<el-table-column prop="url" label="推送参数" width="200" sortable/>
			<el-table-column prop="system" label="系统" sortable/>
			<el-table-column prop="orderCount" label="订单总数" sortable/>
			<el-table-column prop="vipNow" label="VIP用户" sortable/>
			<el-table-column prop="haveCar" label="是否有车" sortable/>
			<el-table-column prop="jiazhao" label="有无驾照" sortable/>
			<el-table-column prop="registerStartTime" label="注册开始时间" width="170" sortable/>
			<el-table-column prop="registerEndTime" label="注册结束时间" width="170" sortable/>
			<el-table-column prop="activityStartTime" label="活跃开始时间" width="170" sortable/>
			<el-table-column prop="activityEndTime" label="活跃结束时间" width="170" sortable/>
			<el-table-column prop="piciNumber" label="批次号" width="170" sortable/>
			
			<el-table-column label="操作" width="300" fixed="right">
					<template scope="scope">
						<div style="padding-left:10px"> 
							<el-button
								size="small"
								:type="'info'"
							>{{scope.row.pushStatus == 1?'推送中':scope.row.pushStatus == 0?'推送完成':'未推送'}}</el-button>
							<el-button size="small" type="success" @click="handlePush(scope.$index, scope.row)">推送</el-button>
							<el-button size="small" @click="handleEdit(slot-scope.$index, scope.row)">编辑</el-button>
							<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
						</div>
					</template>
			</el-table-column>
			<el-table-column prop="insertTime" label="插入时间" width="170" sortable/>
			<el-table-column prop="updateTime" label="更新时间" width="170" sortable/>
		</el-table>
	
		<el-dialog title="新增推送标签" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="标题" prop="title">
					<el-input v-model="addForm.title" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="describe">
					<el-input v-model="addForm.describe"></el-input>
				</el-form-item>
				<el-form-item label="推送参数" prop="url">
					<el-input v-model="addForm.url" placeholder="默认推送是URL方式，请求输入URL"></el-input>
				</el-form-item>
				<el-form-item label="订单总数大于等于" prop="orderCount" style="width:180px;">
					<el-input v-model="addForm.orderCount"></el-input>
				</el-form-item>
				<el-form-item label="VIP用户">
					<template>
						<el-radio v-model="addForm.vipNow" label="1">是</el-radio>
						<el-radio v-model="addForm.vipNow" label="2">否</el-radio>
					</template>
				</el-form-item>
				<el-form-item label="车辆">
					<div style="width:200px;float:left">
						<template>
							<el-radio v-model="addForm.haveCar" label="1">有车</el-radio>
							<el-radio v-model="addForm.haveCar" label="2">无车</el-radio>
						</template>
					</div>
					<div style="width:300px;float:right">
						<span style="padding:20px">驾照</span>
						<template>
							<el-radio v-model="addForm.jiazhao" label="1">有驾照</el-radio>
							<el-radio v-model="addForm.jiazhao" label="2">无驾照</el-radio>
						</template>
					</div>
				</el-form-item>

				<el-form-item label="业务">
					<template>
					<el-radio v-model="addForm.fufei" label="1">有付费记录</el-radio>
					<el-radio v-model="addForm.fufei" label="2">无付费记录</el-radio>
				</template>
				<template>
					<el-radio v-model="addForm.haveRecord" label="1">用过违章业务</el-radio>
					<el-radio v-model="addForm.haveRecord" label="2">没用过违章业务</el-radio>
				</template>
				</el-form-item>
				<el-form-item label="标签名称">
					<el-checkbox-group v-model="checkList">
						<el-checkbox label="user_android">安卓</el-checkbox>
						<el-checkbox label="user_ios">Ios</el-checkbox>
					</el-checkbox-group>
				</el-form-item>
				</div>
				</el-form-item>
				<el-form-item label="注册时间">
					<div style="block">
						<el-date-picker
						v-model="addForm.registerStartTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
						~
						<el-date-picker
						v-model="addForm.registerEndTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
					</div>
				</el-form-item>

				<el-form-item label="活跃时间">
					<div style="block">
						<el-date-picker
						v-model="addForm.activityStartTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
						~
						<el-date-picker
						v-model="addForm.activityEndTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
					</div>
				</el-form-item>
				<el-form-item><span style="color:red;">注意：由于数据量不断在增加且庞大，不能选择今天的用户来推送</span></el-form-item>

				<el-form-item label="备注">
					<el-input type="textarea" v-model="addForm.remarks"></el-input>
				</el-form-item>
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">确定</el-button>
			</div>
		</el-dialog>


		<el-dialog title="点击立即推送" v-model="pushFormVisible" :close-on-click-modal="false">
			<el-form :model="pushForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="标题" prop="title">
					<el-input v-model="pushForm.title" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="describe">
					<el-input v-model="pushForm.describe"></el-input>
				</el-form-item>
				<el-form-item label="推送参数" prop="url">
					<el-input v-model="pushForm.url" placeholder="默认推送是URL方式，请求输入URL"></el-input>
				</el-form-item>
				<el-form-item label="订单总数大于等于" prop="orderCount" style="width:180px;">
					<el-input v-model="pushForm.orderCount"></el-input>
				</el-form-item>
				<el-form-item label="VIP用户">
					<template>
						<el-radio v-model="pushForm.vipNow" label="1">是</el-radio>
						<el-radio v-model="pushForm.vipNow" label="2">否</el-radio>
					</template>
				
				</el-form-item>
				<el-form-item label="车辆">
					<div style="width:200px;float:left">
						<template>
							<el-radio v-model="pushForm.haveCar" label="1">有车</el-radio>
							<el-radio v-model="pushForm.haveCar" label="2">无车</el-radio>
						</template>
					</div>
					<div style="width:300px;float:right">
						<span style="padding:20px">驾照</span>
						<template>
							<el-radio v-model="pushForm.jiazhao" label="1">有驾照</el-radio>
							<el-radio v-model="pushForm.jiazhao" label="2">无驾照</el-radio>
						</template>
					</div>
				</el-form-item>

				<el-form-item label="业务">
					<template>
					<el-radio v-model="pushForm.fufei" label="1">有付费记录</el-radio>
					<el-radio v-model="pushForm.fufei" label="2">无付费记录</el-radio>
				</template>
				<template>
					<el-radio v-model="pushForm.haveRecord" label="1">用过违章业务</el-radio>
					<el-radio v-model="pushForm.haveRecord" label="2">没用过违章业务</el-radio>
				</template>
				</el-form-item>
				<el-form-item label="标签名称">
					<el-checkbox-group v-model="checkList">
						<el-checkbox label="user_android">安卓</el-checkbox>
						<el-checkbox label="user_ios">Ios</el-checkbox>
					</el-checkbox-group>
				</el-form-item>
				</div>
				</el-form-item>
				<el-form-item label="注册时间">
					<div style="block">
						<el-date-picker
						v-model="pushForm.registerStartTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd" disabled="true">
						</el-date-picker>
						~
						<el-date-picker
						v-model="pushForm.registerEndTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd" disabled="true">
						</el-date-picker>
					</div>
				</el-form-item>

				<el-form-item label="活跃时间">
					<div style="block">
						<el-date-picker
						v-model="pushForm.activityStartTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd" disabled="true">
						</el-date-picker>
						~
						<el-date-picker
						v-model="pushForm.activityEndTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd" disabled="true">
						</el-date-picker>
					</div>
				</el-form-item>
				<el-form-item><span style="color:red;">注意：由于数据量不断在增加且庞大，不能选择今天的用户来推送</span></el-form-item>
				<el-form-item><span style="color:red;">注意：登录的用户才有机会收到推送</span></el-form-item>

				<el-form-item label="备注">
					<el-input type="textarea" v-model="pushForm.remarks"></el-input>
				</el-form-item>
			
				<el-form-item label="预计时间" v-if="calculateTime!=''&&calculateTime!=undefine">
					<span><span style="color:red">{{calculateTime}}</span></span>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="pushFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="calculate" :loading="addLoading">计算推送时间</el-button>
				<el-button type="primary" @click.native="pushSubmit" :loading="addLoading">推送</el-button>
			</div>
		</el-dialog>
		<el-dialog title="编辑推送标签" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="标题" prop="title">
					<el-input v-model="editForm.title" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="describe">
					<el-input v-model="editForm.describe"></el-input>
				</el-form-item>
				<el-form-item label="推送参数" prop="url">
					<el-input v-model="editForm.url" placeholder="默认推送是URL方式，请求输入URL"></el-input>
				</el-form-item>
				<el-form-item label="订单总数大于等于" prop="orderCount" style="width:180px;">
					<el-input v-model="editForm.orderCount"></el-input>
				</el-form-item>
				<el-form-item label="VIP用户">
					<template>
						<el-radio v-model="editForm.vipNow" label="1">是</el-radio>
						<el-radio v-model="editForm.vipNow" label="2">否</el-radio>
					</template>
				</el-form-item>
				<el-form-item label="车辆">
					<div style="width:200px;float:left">
						<template>
							<el-radio v-model="editForm.haveCar" label="1">有车</el-radio>
							<el-radio v-model="editForm.haveCar" label="2">无车</el-radio>
						</template>
					</div>
					<div style="width:300px;float:right">
						<span style="padding:20px">驾照</span>
						<template>
							<el-radio v-model="editForm.jiazhao" label="1">有驾照</el-radio>
							<el-radio v-model="editForm.jiazhao" label="2">无驾照</el-radio>
						</template>
					</div>
				</el-form-item>

				<el-form-item label="业务">
					<template>
					<el-radio v-model="editForm.fufei" label="1">有付费记录</el-radio>
					<el-radio v-model="editForm.fufei" label="2">无付费记录</el-radio>
				</template>
				<template>
					<el-radio v-model="editForm.haveRecord" label="1">用过违章业务</el-radio>
					<el-radio v-model="editForm.haveRecord" label="2">没用过违章业务</el-radio>
				</template>
				</el-form-item>
				<el-form-item label="标签名称">
					<el-checkbox-group v-model="checkList">
						<el-checkbox label="user_android">安卓</el-checkbox>
						<el-checkbox label="user_ios">Ios</el-checkbox>
					</el-checkbox-group>
				</el-form-item>
				</div>
				</el-form-item>
				<el-form-item label="注册时间">
					<div style="block">
						<el-date-picker
						v-model="editForm.registerStartTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
						~
						<el-date-picker
						v-model="editForm.registerEndTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
					</div>
				</el-form-item>

				<el-form-item label="活跃时间">
					<div style="block">
						<el-date-picker
						v-model="editForm.activityStartTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
						~
						<el-date-picker
						v-model="editForm.activityEndTime"
						type="date"
						placeholder="选择日期"
						format="yyyy 年 MM 月 dd 日"
						value-format="yyyy-MM-dd">
						</el-date-picker>
					</div>
				</el-form-item>
				<el-form-item><span style="color:red;">注意：由于数据量不断在增加且庞大，不能选择今天的用户来推送</span></el-form-item>


				<el-form-item label="备注">
					<el-input type="textarea" v-model="editForm.remarks"></el-input>
				</el-form-item>
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="addLoading">确定</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	import {
		addTag,
		updateTag,
		deleteTag,
		getTagsList,
		pushTag,
		calculateTag

	} from '../../api/api';
	export default {
		data() {
			return {
				checkList: [],
				calculateTime:'',
				filters: {
					keyword: ''
				},
				value1: '',
				tagsList: [],
				total: 0,
				page: 1,
				pageSize: 20,
				listLoading: false,
				sels: [], //列表选中列
				editFormVisible: false, //编辑界面是否显示
				editLoading: false,
				editFormRules: {
					appName: [{
						required: true,
						message: '请输入应用名称',
						trigger: 'blur'
					}],
				},
				//编辑界面数据
				editForm: {
					title: '',
					describe: '',
					remarks: '',
					url:"",
					vipNow:0,
					haveCar:0,
					jiazhao:0,
					fufei:0,
					haveRecord:0,
					activityStartTime: '',
					activityEndTime: '',
					registerStartTime: '',
					registerEndTime: '',
				},
				pushForm: {
					title: '',
					describe: '',
					remarks: '',
					url:"",
					vipNow:0,
					haveCar:0,
					jiazhao:0,
					fufei:0,
					haveRecord:0,
					activityStartTime: '',
					activityEndTime: '',
					registerStartTime: '',
					registerEndTime: '',
					piciNumber:'',
				},
				addFormVisible: false, //新增界面是否显示
				addLoading: false,
				pushFormVisible:false,
				addFormRules: {
					title: [{
						required: true,
						message: '请输入推送标题',
						trigger: 'blur'
					}],
					describe: [{
						required: true,
						message: '请输入推送描述',
						trigger: 'blur'
					}]
				},
				//新增界面数据
				addForm: {
					title: '',
					describe: '',
					remarks: '',
					url:"",
					vipNow:0,
					haveCar:0,
					jiazhao:0,
					fufei:0,
					haveRecord:0,
					activityStartTime: '',
					activityEndTime: '',
					registerStartTime: '',
					registerEndTime: '',
				}


			}
		},
		methods: {
			handleCurrentChange(val) {
				this.page = val;
				this.getList();
			},
			handlePush(index, row){
				this.pushFormVisible = true;
				this.calculateTime = ''
				this.pushForm = Object.assign({}, row);
				this.checkList=this.pushForm.system.split(",")
			},
			//获取用户列表
			getList() {
				this.listLoading = true;
				getTagsList().then((res) => {
					let result = res.data
					if (result.code == "1000") {
						this.tagsList = res.data.data.tags.map(item => {
							return Object.assign({}, item, {
								insertTime: util.formatDate.format(new Date(item.insertTime), 'yyyy-MM-dd hh:mm:ss'),
								updateTime: util.formatDate.format(new Date(item.updateTime), 'yyyy-MM-dd hh:mm:ss'),
							})
						});
					}
					this.listLoading = false;
				});
			},
			//删除
			handleDel: function(index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = {
						id: row.id
					};
					deleteTag(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getList();
					});
				}).catch(() => {});
			},
			//显示编辑界面
			handleEdit: function(index, row) {
				this.editFormVisible = true;
				console.log("--",row);
				this.editForm = Object.assign({}, row);
				this.checkList=this.editForm.system.split(",")
			},
			//显示新增界面
			handleAdd: function() {
				this.addForm =  {
									title: '',
									describe: '',
									remarks: '',
									url:"",
									vipNow:0,
									haveCar:0,
									jiazhao:0,
									fufei:0,
									haveRecord:0,
									activityStartTime: '',
									activityEndTime: '',
									registerStartTime: '',
									registerEndTime: '',
								};
				this.calculateTime = '';
				this.addFormVisible = true;
			},
			//编辑
			editSubmit: function() {
				this.$confirm('确认提交吗？', '提示', {}).then(() => {
					this.editLoading = true;
					var str = "";
					this.checkList.forEach(function(element) {
						str += element+",";
					}, this);
					str = str.substring(0,str.length-1);
					this.editForm.checkList = str
					console.log("-----------------",this.editForm)
					updateTag(this.editForm).then((res) => {
						this.editLoading = false;
						this.$message({
							message: '提交成功',
							type: 'success'
						});
						this.editFormVisible = false;
						this.getList();

					});
				});
			},
			//新增
			addSubmit: function() {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							var str = "";
							this.checkList.forEach(function(element) {
								str += element+",";
							}, this);
							str = str.substring(0,str.length-1);
							this.addForm.checkList = str;
							let para = Object.assign({}, this.addForm);
							console.log("列表",para)
							addTag(para).then((res) => {
								this.addLoading = false;
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getList();
								
							});
						});
					}
				});
			},
			selsChange: function(sels) {
				this.sels = sels;
			},
			calculate:function(){
				this.$confirm('确认进行计算数据吗？', '提示', {}).then(() => {
					this.editLoading = true;
					var str = "";
					this.checkList.forEach(function(element) {
						str += element+",";
					}, this);
					str = str.substring(0,str.length-1);
					this.pushForm.checkList = str
					console.log("-----------------",this.pushForm)
					this.calculateTime = ''
					calculateTag(this.pushForm).then((res) => {
						this.editLoading = false;
						
						console.log(res.data.data)
						this.calculateTime = res.data.msg
						this.$message({
							message: '计算成功',
							type: 'success'
						});
					});
				});
			},
			pushSubmit:function(){
				this.$confirm('确认提交推送吗？', '提示', {}).then(() => {
					this.addLoading = true;
					var str = "";
					this.checkList.forEach(function(element) {
						str += element+",";
					}, this);
					str = str.substring(0,str.length-1);
					this.pushForm.checkList = str;
					let para = Object.assign({}, this.pushForm);
					console.log("列表",para)
					pushTag(para).then((res) => {
						this.addLoading = false;
						if(res.code=='1000'){
							this.$message({
								message: res.data.msg,
								type: 'success'
							});
						}else{
							this.$message({
							message: res.data.msg,
							type: 'warning'
							});
						}
					});
				});
			},
		},
		
		mounted() {
			this.getList();
		}
	}
</script>

<style scoped>

</style>